# RecursiveTree.py # # Description: Draws a tree recursively. # # Author: AL and AL # Date: Oct. 20 2023 # Import the turtle library import turtle # Define a recurisve function that draws a tree def drawTree(aTurtle, aLevel, aBranchLength): '''Draws a tree recursively where "aTurtle" is the turtle drawing the tree, "aLevel" is the number of levels of branches and "aBranchLength" the length of branch to draw. ''' # Base Case: # If we are at the leaf level (level == 0), draw a green leaf! if aLevel == 0: aTurtle.color("green") aTurtle.stamp() aTurtle.color("brown") else: # Recursive Case: # Draw a branch aTurtle.forward(aBranchLength) # Turn left and draw a smaller tree aTurtle.left(40) drawTree(aTurtle, aLevel - 1, aBranchLength/1.5) # Turn right and draw a smaller tree aTurtle.right(80) # 40 + 40 drawTree(aTurtle, aLevel - 1, aBranchLength/1.5) # Go back aTurtle.left(40) aTurtle.back(aBranchLength) return # ***Main part of my program # Creates a graphics window "canvas" canvas = turtle.Screen() # Create a turtle named "tt" tt = turtle.Turtle() # Set up our turtle "tt" tt.color("brown") tt.width(3) tt.shape("triangle") # Move our turtle "tt" tt.speed(6) tt.penup() tt.goto(0, -180) tt.left(90) tt.pendown() theBranchLength = 100 tt.forward(theBranchLength) # Call our recursive function theLevel = 5 drawTree(tt, theLevel, theBranchLength) tt.back(theBranchLength) # Click on the canvas to exit canvas.exitonclick()